package com.mud.dnd.frontend.rs;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.ejb.UserManager;
import com.mud.dnd.ejb.common.AuthenticationException;

@Stateless
@Path("/User")
public class UserJSONService {

	@EJB
	UserManager ejbService;
	
	

	@GET
	@Path("/login")
	@Produces(MediaType.APPLICATION_JSON)
	public String loginUser(@QueryParam("name") String name,@QueryParam("pass") String password) {

		String sessionID="";
		
		try {
			sessionID =  ejbService.loginUser(name, password);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
		
		return "OK Session ID:"+sessionID;

	}

	

	@GET
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public String createUser(@QueryParam("name") String name,@QueryParam("pass") String password) {
		int resultID;
		
		try {
			resultID = ejbService.createUser(name, password);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
		
		return "OK the new user ID:"+resultID;
	}

	

	@GET
	@Path("/delete")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteUser(@QueryParam("name") String name,@QueryParam("targetName") String targetName,@QueryParam("sessionID") String sessionID){
		try {
			ejbService.deleteUser(name, targetName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
		return "OK";
	}

	

	@GET
	@Path("/logout")
	@Produces(MediaType.APPLICATION_JSON)
	public String logoutUser(@QueryParam("name") String name,@QueryParam("sessionID") String sessionID)  {
		try {
			ejbService.logoutUser(name, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
		
		return "OK";

}
}
